package com.gaogzhen.communication.waitNotify;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "w.WaitNotifyTest01")
public class WaitNotifyTest01 {
    final static Object obj = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (obj) {
                log.debug("执行代码...");
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("{} 线程其他代码...", Thread.currentThread().getName());
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (obj) {
                log.debug("执行代码...");
                try {
                    // 等待条件
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("{} 线程其他代码...", Thread.currentThread().getName());
            }
        }, "t2").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            synchronized (obj) {
//                log.debug("条件满足，通知可以继续执行...");
//                obj.notify();

                log.debug("条件都满足，通知全部继续执行...");
                obj.notifyAll();
            }
        }, "t3").start();
    }
}
